package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

func postFrom() {
	//fromdata 形式与querystring 类似
	data := make(url.Values)
	data.Add("name", "ding")
	data.Add("age", "20")
	s := data.Encode()
	r, err := http.Post(
		"http://httpbin.org/post",
		"application/x-www-form-urlencoded",
		strings.NewReader(s),
	)
	if err != nil {
		panic(err)
	}
	defer r.Body.Close()
	b, err2 := ioutil.ReadAll(r.Body)
	if err2 != nil {
		panic(err2)
	}
	fmt.Printf("%s", b)

}

func postJson() {
	u := struct {
		Name string `json:"name"`
		Age  int    `json:"age"`
	}{
		Name: "ding",
		Age:  20,
	}
	s, err := json.Marshal(u)
	if err != nil {
		panic(err)
	}
	r, err := http.Post(
		"http://httpbin.org/post",
		"application/json",
		bytes.NewReader(s),
	)
	if err != nil {
		panic(err)
	}
	defer r.Body.Close()
	b, err2 := ioutil.ReadAll(r.Body)
	if err2 != nil {
		panic(err2)
	}
	fmt.Printf("%s", b)
}
func main() {
	postFrom()
}
